﻿using System.Collections.Generic;
using System.Text;
using Obsidian.Action;
using Obsidian.Edm;
using [#BaseNS#];
using [#LgNS#];
using [#ModelNS#];

/*============================
 * @author [#AUTHOR#]
 * @created [#CREATED#]
 * @desc 
* ============================ */

namespace [#ServNS#]
{
    /// <summary>
    /// [#ClsName#] 接口服务
    /// </summary>
    public class [#ClsName#]Serv : [#ClsBaseServ#]
    {
        #region [字段属性]
		private const string RESULT = "result";   //结果集参数名称（主要用于删除接口）
        private const string RESULT_FIELDS = "code,msg";  //结果集参数对应字段（主要用于删除接口）
        private const string QUERY = "qry";   //请求列表主参数名称（主要用于查询列表接口）
        private const string LISTATTR = "listAttr";  //返回分页参数名称
        private const string LISTATTR_FIELDS = "itemsCount,pageNo,pageSize,pagesCount";  //分页结果集字段

        private const string ENTITY_REQ = "[#sClsName#]"; //[请求]主参数名称
        private const string ENTITY_RES = "[#sClsName#]s"; //[返回]主参数名称
        
		//字段别名串
        private static string [#sClsName#]Fields;
        private static string [#ClsName#]Fields
        {
            get
            {
                if (string.IsNullOrEmpty([#sClsName#]Fields))
                {
                    StringBuilder sb = new StringBuilder();
                    [#ClsName#]Info buildingLevel = new [#ClsName#]Info();
                    IModelField[] fields = buildingLevel.Fields;
                    bool first = true;
                    foreach (IModelField field in fields)
                    {
                        if (first) first = false;
                        else sb.Append(",");
                        sb.Append(field.Alias);
                    }
                    [#sClsName#]Fields = sb.ToString();
                }
                return [#sClsName#]Fields;
            }
        }
        #endregion [字段属性]

        public [#ClsName#]Serv([#ClsBaseSession#] session): base(session){}

		#region [获取]
        public void Get(ActionRequest req, ActionResponse res)
        {
            string msg = "";

            ActReqParam param;
            if (!req.TryGetParam(ENTITY_REQ, out param))
            {
                res.Error("参数" + ENTITY_REQ + "错误");
                return;
            }
            [#IdSysType#] [#IdAlias#] = 0;
            if (!param.[#TryGetFirstType#]("[#IdAlias#]", out [#IdAlias#]))
            {
                res.Error("参数" + ENTITY_REQ + "属性[#IdAlias#]错误");
                return;
            }
            [#ClsName#]LG lg = new [#ClsName#]LG(Session);
            [#ClsName#]Info ei = lg.Get[#ClsName#]([#IdAlias#], out msg);
            if (ei == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult ar = res.AddResult(ENTITY_RES, [#ClsName#]Fields);
            ar.AddModel(ei);
        }
		#endregion [获取]

		#region [列表]
        public void List(ActionRequest req, ActionResponse res)
        {
            string msg = "";
            ListAttrInfo la;

            [#ClsName#]Qry qry = req.GetModelByNameOrFirst<[#ClsName#]Qry>(QUERY);
            [#ClsName#]LG lg = new [#ClsName#]LG(Session);
            List<[#ClsName#]Info> list = lg.Get[#ClsName#]s(qry, out la, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult ar = res.AddResult(ENTITY_RES, [#ClsName#]Fields);
            ar.AddModels<[#ClsName#]Info>(list);
			
            ActionResult arAttr = res.AddResult(LISTATTR, LISTATTR_FIELDS);
            arAttr.AddModel(la);
        }
		#endregion [列表]

		#region [新增]
        public void Add(ActionRequest req, ActionResponse res)
        {
			if (!Session.IsLogin)
            {
                res.SessionInvalid();
                return;
            }
			
            string msg = "";
            [#ClsName#]LG lg = new [#ClsName#]LG(Session);
            [#ClsName#]Info ei = req.GetModelByNameOrFirst<[#ClsName#]Info>(ENTITY_REQ);
            ei = lg.Add[#ClsName#](ei, out msg);
            if (ei == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult ar = res.AddResult(ENTITY_RES, [#ClsName#]Fields);
            ar.AddModel(ei);
        }
        #endregion [新增]

        #region [更新]
        public void Update(ActionRequest req, ActionResponse res)
        {
			if (!Session.IsLogin)
            {
                res.SessionInvalid();
                return;
            }
			
            string msg = "";

            [#ClsName#]Info ei = req.GetModelByNameOrFirst<[#ClsName#]Info>(ENTITY_REQ);
            [#ClsName#]LG lg = new [#ClsName#]LG(Session);
            ei = lg.Update[#ClsName#](ei, out msg);
            if (ei == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult ar = res.AddResult(ENTITY_RES, [#ClsName#]Fields);
            ar.AddModel(ei);
        }
        #endregion [更新]
		
		#region [删除]
        public void Delete(ActionRequest req, ActionResponse res)
        {
			if (!Session.IsAdmin) //管理员权限
            {
                res.SessionInvalid();
                return;
            }
			
            string msg = "";

            ActReqParam param;
            if (!req.TryGetParam(ENTITY_REQ, out param))
            {
                res.Error("参数" + ENTITY_REQ + "错误");
                return;
            }
            [#IdSysType#] [#IdAlias#] = 0;
            if (!param.[#TryGetType#](0, "[#IdAlias#]", out [#IdAlias#]))
            {
                res.Error("参数" + ENTITY_REQ + "属性[#IdAlias#]错误");
                return;
            }

            [#ClsName#]LG lg = new [#ClsName#]LG(Session);
            int code = 0;
            code = lg.Delete[#ClsName#]([#IdAlias#], out msg);
            if (code <= 0)
            {
                res.Error(msg);
                return;
            }
            ActionResult ar = res.AddResult(RESULT, RESULT_FIELDS);
            ar.AddValues(code, msg);
        }
        #endregion [删除]
    }
}